# Replication code for Taylor C. Boas (2014), "Pastor Paulo vs. Doctor Carlos: Professional Titles as Voting Heuristics in Brazil," Journal of Politics in Latin America 6, 2: 39-72. This article analyzes the 2012 survey "Religion, Race, and Class in Brazilian Municipal Elections." The dataset and codebook (without the additional replication materials included here) can also be obtained separately from the Harvard Dataverse Network, http://thedata.harvard.edu/dvn/.

rm(list=ls(all=T))

# Set working directory as appropriate

setwd('~/Dropbox/Current Research/Pastor Paulo/replication/')

# Load packages

library(Matching)
library(Hmisc)
library(foreign)
library(rms)

# Load replication file. It contains the following objects:
#
# 1. survey: data from the "Religion, Race, and Class in Brazilian Municipal Elections" 2012 survey, with the following additional variables merged in:
#	codeibge: IBGE code for respondent's municipality
#	codetse: TSE code for respondent's municipality
#	pastors: Number of 2012 vereador candidates with a "pastor" ballot name in respondent's municipality (from TSE)
#	doctors: Number of 2012 vereador candidates with a "doctor" ballot name in respondent's municipality (from TSE)
#	lat: Latitude of respondent's municipality (from Google Maps)
#	lng: Longitude of respondent's municipality (from Google Maps)
#	catholic.muni: Percent Catholic of respondent's municipality (from IBGE, 2010 census)
#	evangelical.muni: Percent evangelical of respondent's municipality (from IBGE, 2010 census)
#	pop.muni, log.pop.muni: Population and logged population of respondent's municipality (from IBGE, 2010 census)
#	region: Region of the country
#
# 2. clergy: database of clergy candidates for vereador (those listing their occupation as SACERDOTE OU MEMBRO DE ORDEM OU SEITA RELIGIOSA) in the 2000, 2004, 2008, and 2012 elections (from TSE). The following additional variables were created:
#	pastor: indicator for a candidate's ballot name containing the title "Pastor" (or an abbreviated version)
#	clergy.cands.muni: total number of clergy candidates for vereador in the candidate's municipality in that election
#	evangelical: percent evangelical of candidate's municipality (from IGBE, 2000 and 2010 census, interpolated/extrapolated for 2004, 2008, and 2012)
#	catholic: percent Catholic of candidate's municipality (from IGBE, 2000 and 2010 census, interpolated/extrapolated for 2004, 2008, and 2012)
#
# 3. munis12: database of Brazilian municipalities (from TSE, 2012). Contains the following additional variables:
#	pastors: Number of 2012 vereador candidates with a "pastor" ballot name in the municipality
#	doctors: Number of 2012 vereador candidates with a "doctor" ballot name in the municipality
#	codeibge: IBGE code for respondent's municipality
#
# 4. age.pop, income.pop, muni.pop, race.pop, region.pop, relig.pop: Population figures for ages 18 and older--by sex, income bracket, municipality, race, region, and religion (from IBGE, 2010 census).
#
# 5. pres2010, turnout2010, vote2010: results of 2010 presidential election (from TSE).

load('pastor_paulo_replication.RData')

# ========================================
# Set up survey file, create new variables
# ========================================

survey[survey== -99]<-NA
survey<-survey[survey$StartDate < as.POSIXlt('2012-10-07'),] # Only pre-election respondents

survey$north<-survey$region=='Norte'
survey$northeast<-survey$region=='Nordeste'
survey$centerwest<-survey$region=='Centro-Oeste'
survey$southeast<-survey$region=='Sudeste'
survey$south<-survey$region=='Sul'

survey$catholic<-survey$Q16==1
survey$evangelical<-survey$Q16==2
survey$none<-survey$Q16==3
survey$atheist<-survey$Q16==4
survey$other<-survey$Q16==5
survey$pentecostal<-survey$Q17==1&survey$evangelical
survey$charismatic<-survey$Q17==2&survey$catholic

survey$white<-survey$Q25==1
survey$black<-survey$Q25==2
survey$brown<-survey$Q25==3
survey$yellow<-survey$Q25==4
survey$indigenous<-survey$Q25==5

survey$male<-survey$Q27==1

survey$church<- 6 - survey$Q19

survey$noparty<-survey$Q21 == 2
survey$PT<-survey$Q22 %in% 25
survey$PT[is.na(survey$Q21)]<-NA
survey$PSDB<-survey$Q22 %in% 20
survey$PSDB[is.na(survey$Q21)]<-NA
survey$PMDB<-survey$Q22 %in% 8
survey$PMDB[is.na(survey$Q21)]<-NA
survey$otherparty<-survey$Q22 %in% c(1:7,9:19,21:24,26:30)
survey$otherparty[is.na(survey$Q21)]<-NA

survey$minwage0_2<-survey$Q29==1
survey$minwage2_5<-survey$Q29==2
survey$minwage5_8<-survey$Q29==3
survey$minwage8_12<-survey$Q29==4
survey$minwage12_15<-survey$Q29==5
survey$minwage15_20<-survey$Q29==6
survey$minwage20plus<-survey$Q29==7
survey$minwage15plus<-survey$Q29>=6

survey$pastor.treat.type<-NA
survey$pastor.treat.type<-1*(survey$pastors>0&survey$Q9_number==99999)+2*(survey$pastors==0)+3*(survey$pastors>0&survey$Q9_number!=99999)
survey$pastor.treat.type<-factor(survey$pastor.treat.type,labels=c('fake candidate','no candidate','real candidate'))

survey$doctor.treat.type<-NA
survey$doctor.treat.type<-1*(survey$doctors>0&survey$Q10_number==99999)+2*(survey$doctors==0)+3*(survey$doctors>0&survey$Q10_number!=99999)
survey$doctor.treat.type<-factor(survey$doctor.treat.type,labels=c('fake candidate','no candidate','real candidate'))

survey$screener1.pass<-survey$Q12_ClickCount==6|(survey$Q12_ClickCount>6&is.na(survey$Q12_1)&is.na(survey$Q12_2))
survey$screener2.pass<-survey$Q20_k==1
survey$screener<-factor(apply(survey[,c('screener1.pass','screener2.pass')],1,sum,na.rm=T),labels=c('None','One','Both'))

# ========
# Figure 1
# ========

# Coefficients for pastor vote intention

reg.pastor.vote.all<-lm(Q9_1~Q9_treat,data=survey)
diff.pastor.vote.all<-coef(reg.pastor.vote.all)['Q9_treat']
se.pastor.vote.all<-sqrt(vcov(reg.pastor.vote.all)['Q9_treat','Q9_treat'])
df.pastor.vote.all<-reg.pastor.vote.all$df.residual

reg.pastor.vote.evang<-lm(Q9_1~Q9_treat,data=survey[survey$evangelical,])
diff.pastor.vote.evang<-coef(reg.pastor.vote.evang)['Q9_treat']
se.pastor.vote.evang<-sqrt(vcov(reg.pastor.vote.evang)['Q9_treat','Q9_treat'])
df.pastor.vote.evang<-reg.pastor.vote.evang$df.residual

reg.pastor.vote.pent<-lm(Q9_1~Q9_treat,data=survey[survey$pentecostal,])
diff.pastor.vote.pent<-coef(reg.pastor.vote.pent)['Q9_treat']
se.pastor.vote.pent<-sqrt(vcov(reg.pastor.vote.pent)['Q9_treat','Q9_treat'])
df.pastor.vote.pent<-reg.pastor.vote.pent$df.residual

reg.pastor.vote.cath<-lm(Q9_1~Q9_treat,data=survey[survey$catholic,])
diff.pastor.vote.cath<-coef(reg.pastor.vote.cath)['Q9_treat']
se.pastor.vote.cath<-sqrt(vcov(reg.pastor.vote.cath)['Q9_treat','Q9_treat'])
df.pastor.vote.cath<-reg.pastor.vote.cath$df.residual

reg.pastor.vote.caris<-lm(Q9_1~Q9_treat,data=survey[survey$charismatic,])
diff.pastor.vote.caris<-coef(reg.pastor.vote.caris)['Q9_treat']
se.pastor.vote.caris<-sqrt(vcov(reg.pastor.vote.caris)['Q9_treat','Q9_treat'])
df.pastor.vote.caris<-reg.pastor.vote.caris$df.residual

reg.pastor.vote.none<-lm(Q9_1~Q9_treat,data=survey[survey$none,])
diff.pastor.vote.none<-coef(reg.pastor.vote.none)['Q9_treat']
se.pastor.vote.none<-sqrt(vcov(reg.pastor.vote.none)['Q9_treat','Q9_treat'])
df.pastor.vote.none<-reg.pastor.vote.none$df.residual

reg.pastor.vote.atheist<-lm(Q9_1~Q9_treat,data=survey[survey$atheist,])
diff.pastor.vote.atheist<-coef(reg.pastor.vote.atheist)['Q9_treat']
se.pastor.vote.atheist<-sqrt(vcov(reg.pastor.vote.atheist)['Q9_treat','Q9_treat'])
df.pastor.vote.atheist<-reg.pastor.vote.atheist$df.residual

reg.pastor.vote.other<-lm(Q9_1~Q9_treat,data=survey[survey$other,])
diff.pastor.vote.other<-coef(reg.pastor.vote.other)['Q9_treat']
se.pastor.vote.other<-sqrt(vcov(reg.pastor.vote.other)['Q9_treat','Q9_treat'])
df.pastor.vote.other<-reg.pastor.vote.other$df.residual

diff.pastor.vote<-c(diff.pastor.vote.all,diff.pastor.vote.evang,diff.pastor.vote.pent,diff.pastor.vote.cath,diff.pastor.vote.caris,diff.pastor.vote.none,diff.pastor.vote.atheist,diff.pastor.vote.other)
se.pastor.vote<-c(se.pastor.vote.all,se.pastor.vote.evang,se.pastor.vote.pent,se.pastor.vote.cath,se.pastor.vote.caris,se.pastor.vote.none,se.pastor.vote.atheist,se.pastor.vote.other)
df.pastor.vote<-c(df.pastor.vote.all,df.pastor.vote.evang,df.pastor.vote.pent,df.pastor.vote.cath,df.pastor.vote.caris,df.pastor.vote.none,df.pastor.vote.atheist,df.pastor.vote.other)

# Coefficients for pastor competence

reg.pastor.comp.all<-lm(Q9_2~Q9_treat,data=survey)
diff.pastor.comp.all<-coef(reg.pastor.comp.all)['Q9_treat']
se.pastor.comp.all<-sqrt(vcov(reg.pastor.comp.all)['Q9_treat','Q9_treat'])
df.pastor.comp.all<-reg.pastor.comp.all$df.residual

reg.pastor.comp.evang<-lm(Q9_2~Q9_treat,data=survey[survey$evangelical,])
diff.pastor.comp.evang<-coef(reg.pastor.comp.evang)['Q9_treat']
se.pastor.comp.evang<-sqrt(vcov(reg.pastor.comp.evang)['Q9_treat','Q9_treat'])
df.pastor.comp.evang<-reg.pastor.comp.evang$df.residual

reg.pastor.comp.pent<-lm(Q9_2~Q9_treat,data=survey[survey$pentecostal,])
diff.pastor.comp.pent<-coef(reg.pastor.comp.pent)['Q9_treat']
se.pastor.comp.pent<-sqrt(vcov(reg.pastor.comp.pent)['Q9_treat','Q9_treat'])
df.pastor.comp.pent<-reg.pastor.comp.pent$df.residual

reg.pastor.comp.cath<-lm(Q9_2~Q9_treat,data=survey[survey$catholic,])
diff.pastor.comp.cath<-coef(reg.pastor.comp.cath)['Q9_treat']
se.pastor.comp.cath<-sqrt(vcov(reg.pastor.comp.cath)['Q9_treat','Q9_treat'])
df.pastor.comp.cath<-reg.pastor.comp.cath$df.residual

reg.pastor.comp.caris<-lm(Q9_2~Q9_treat,data=survey[survey$charismatic,])
diff.pastor.comp.caris<-coef(reg.pastor.comp.caris)['Q9_treat']
se.pastor.comp.caris<-sqrt(vcov(reg.pastor.comp.caris)['Q9_treat','Q9_treat'])
df.pastor.comp.caris<-reg.pastor.comp.caris$df.residual

reg.pastor.comp.none<-lm(Q9_2~Q9_treat,data=survey[survey$none,])
diff.pastor.comp.none<-coef(reg.pastor.comp.none)['Q9_treat']
se.pastor.comp.none<-sqrt(vcov(reg.pastor.comp.none)['Q9_treat','Q9_treat'])
df.pastor.comp.none<-reg.pastor.comp.none$df.residual

reg.pastor.comp.atheist<-lm(Q9_2~Q9_treat,data=survey[survey$atheist,])
diff.pastor.comp.atheist<-coef(reg.pastor.comp.atheist)['Q9_treat']
se.pastor.comp.atheist<-sqrt(vcov(reg.pastor.comp.atheist)['Q9_treat','Q9_treat'])
df.pastor.comp.atheist<-reg.pastor.comp.atheist$df.residual

reg.pastor.comp.other<-lm(Q9_2~Q9_treat,data=survey[survey$other,])
diff.pastor.comp.other<-coef(reg.pastor.comp.other)['Q9_treat']
se.pastor.comp.other<-sqrt(vcov(reg.pastor.comp.other)['Q9_treat','Q9_treat'])
df.pastor.comp.other<-reg.pastor.comp.other$df.residual

diff.pastor.comp<-c(diff.pastor.comp.all,diff.pastor.comp.evang,diff.pastor.comp.pent,diff.pastor.comp.cath,diff.pastor.comp.caris,diff.pastor.comp.none,diff.pastor.comp.atheist,diff.pastor.comp.other)
se.pastor.comp<-c(se.pastor.comp.all,se.pastor.comp.evang,se.pastor.comp.pent,se.pastor.comp.cath,se.pastor.comp.caris,se.pastor.comp.none,se.pastor.comp.atheist,se.pastor.comp.other)
df.pastor.comp<-c(df.pastor.comp.all,df.pastor.comp.evang,df.pastor.comp.pent,df.pastor.comp.cath,df.pastor.comp.caris,df.pastor.comp.none,df.pastor.comp.atheist,df.pastor.comp.other)

# Coefficients for pastor intelligence

reg.pastor.intel.all<-lm(Q9_3~Q9_treat,data=survey)
diff.pastor.intel.all<-coef(reg.pastor.intel.all)['Q9_treat']
se.pastor.intel.all<-sqrt(vcov(reg.pastor.intel.all)['Q9_treat','Q9_treat'])
df.pastor.intel.all<-reg.pastor.intel.all$df.residual

reg.pastor.intel.evang<-lm(Q9_3~Q9_treat,data=survey[survey$evangelical,])
diff.pastor.intel.evang<-coef(reg.pastor.intel.evang)['Q9_treat']
se.pastor.intel.evang<-sqrt(vcov(reg.pastor.intel.evang)['Q9_treat','Q9_treat'])
df.pastor.intel.evang<-reg.pastor.intel.evang$df.residual

reg.pastor.intel.pent<-lm(Q9_3~Q9_treat,data=survey[survey$pentecostal,])
diff.pastor.intel.pent<-coef(reg.pastor.intel.pent)['Q9_treat']
se.pastor.intel.pent<-sqrt(vcov(reg.pastor.intel.pent)['Q9_treat','Q9_treat'])
df.pastor.intel.pent<-reg.pastor.intel.pent$df.residual

reg.pastor.intel.cath<-lm(Q9_3~Q9_treat,data=survey[survey$catholic,])
diff.pastor.intel.cath<-coef(reg.pastor.intel.cath)['Q9_treat']
se.pastor.intel.cath<-sqrt(vcov(reg.pastor.intel.cath)['Q9_treat','Q9_treat'])
df.pastor.intel.cath<-reg.pastor.intel.cath$df.residual

reg.pastor.intel.caris<-lm(Q9_3~Q9_treat,data=survey[survey$charismatic,])
diff.pastor.intel.caris<-coef(reg.pastor.intel.caris)['Q9_treat']
se.pastor.intel.caris<-sqrt(vcov(reg.pastor.intel.caris)['Q9_treat','Q9_treat'])
df.pastor.intel.caris<-reg.pastor.intel.caris$df.residual

reg.pastor.intel.none<-lm(Q9_3~Q9_treat,data=survey[survey$none,])
diff.pastor.intel.none<-coef(reg.pastor.intel.none)['Q9_treat']
se.pastor.intel.none<-sqrt(vcov(reg.pastor.intel.none)['Q9_treat','Q9_treat'])
df.pastor.intel.none<-reg.pastor.intel.none$df.residual

reg.pastor.intel.atheist<-lm(Q9_3~Q9_treat,data=survey[survey$atheist,])
diff.pastor.intel.atheist<-coef(reg.pastor.intel.atheist)['Q9_treat']
se.pastor.intel.atheist<-sqrt(vcov(reg.pastor.intel.atheist)['Q9_treat','Q9_treat'])
df.pastor.intel.atheist<-reg.pastor.intel.atheist$df.residual

reg.pastor.intel.other<-lm(Q9_3~Q9_treat,data=survey[survey$other,])
diff.pastor.intel.other<-coef(reg.pastor.intel.other)['Q9_treat']
se.pastor.intel.other<-sqrt(vcov(reg.pastor.intel.other)['Q9_treat','Q9_treat'])
df.pastor.intel.other<-reg.pastor.intel.other$df.residual

diff.pastor.intel<-c(diff.pastor.intel.all,diff.pastor.intel.evang,diff.pastor.intel.pent,diff.pastor.intel.cath,diff.pastor.intel.caris,diff.pastor.intel.none,diff.pastor.intel.atheist,diff.pastor.intel.other)
se.pastor.intel<-c(se.pastor.intel.all,se.pastor.intel.evang,se.pastor.intel.pent,se.pastor.intel.cath,se.pastor.intel.caris,se.pastor.intel.none,se.pastor.intel.atheist,se.pastor.intel.other)
df.pastor.intel<-c(df.pastor.intel.all,df.pastor.intel.evang,df.pastor.intel.pent,df.pastor.intel.cath,df.pastor.intel.caris,df.pastor.intel.none,df.pastor.intel.atheist,df.pastor.intel.other)

# Coefficients for pastor honesty

reg.pastor.honest.all<-lm(Q9_4~Q9_treat,data=survey)
diff.pastor.honest.all<-coef(reg.pastor.honest.all)['Q9_treat']
se.pastor.honest.all<-sqrt(vcov(reg.pastor.honest.all)['Q9_treat','Q9_treat'])
df.pastor.honest.all<-reg.pastor.honest.all$df.residual

reg.pastor.honest.evang<-lm(Q9_4~Q9_treat,data=survey[survey$evangelical,])
diff.pastor.honest.evang<-coef(reg.pastor.honest.evang)['Q9_treat']
se.pastor.honest.evang<-sqrt(vcov(reg.pastor.honest.evang)['Q9_treat','Q9_treat'])
df.pastor.honest.evang<-reg.pastor.honest.evang$df.residual

reg.pastor.honest.pent<-lm(Q9_4~Q9_treat,data=survey[survey$pentecostal,])
diff.pastor.honest.pent<-coef(reg.pastor.honest.pent)['Q9_treat']
se.pastor.honest.pent<-sqrt(vcov(reg.pastor.honest.pent)['Q9_treat','Q9_treat'])
df.pastor.honest.pent<-reg.pastor.honest.pent$df.residual

reg.pastor.honest.cath<-lm(Q9_4~Q9_treat,data=survey[survey$catholic,])
diff.pastor.honest.cath<-coef(reg.pastor.honest.cath)['Q9_treat']
se.pastor.honest.cath<-sqrt(vcov(reg.pastor.honest.cath)['Q9_treat','Q9_treat'])
df.pastor.honest.cath<-reg.pastor.honest.cath$df.residual

reg.pastor.honest.caris<-lm(Q9_4~Q9_treat,data=survey[survey$charismatic,])
diff.pastor.honest.caris<-coef(reg.pastor.honest.caris)['Q9_treat']
se.pastor.honest.caris<-sqrt(vcov(reg.pastor.honest.caris)['Q9_treat','Q9_treat'])
df.pastor.honest.caris<-reg.pastor.honest.caris$df.residual

reg.pastor.honest.none<-lm(Q9_4~Q9_treat,data=survey[survey$none,])
diff.pastor.honest.none<-coef(reg.pastor.honest.none)['Q9_treat']
se.pastor.honest.none<-sqrt(vcov(reg.pastor.honest.none)['Q9_treat','Q9_treat'])
df.pastor.honest.none<-reg.pastor.honest.none$df.residual

reg.pastor.honest.atheist<-lm(Q9_4~Q9_treat,data=survey[survey$atheist,])
diff.pastor.honest.atheist<-coef(reg.pastor.honest.atheist)['Q9_treat']
se.pastor.honest.atheist<-sqrt(vcov(reg.pastor.honest.atheist)['Q9_treat','Q9_treat'])
df.pastor.honest.atheist<-reg.pastor.honest.atheist$df.residual

reg.pastor.honest.other<-lm(Q9_4~Q9_treat,data=survey[survey$other,])
diff.pastor.honest.other<-coef(reg.pastor.honest.other)['Q9_treat']
se.pastor.honest.other<-sqrt(vcov(reg.pastor.honest.other)['Q9_treat','Q9_treat'])
df.pastor.honest.other<-reg.pastor.honest.other$df.residual

diff.pastor.honest<-c(diff.pastor.honest.all,diff.pastor.honest.evang,diff.pastor.honest.pent,diff.pastor.honest.cath,diff.pastor.honest.caris,diff.pastor.honest.none,diff.pastor.honest.atheist,diff.pastor.honest.other)
se.pastor.honest<-c(se.pastor.honest.all,se.pastor.honest.evang,se.pastor.honest.pent,se.pastor.honest.cath,se.pastor.honest.caris,se.pastor.honest.none,se.pastor.honest.atheist,se.pastor.honest.other)
df.pastor.honest<-c(df.pastor.honest.all,df.pastor.honest.evang,df.pastor.honest.pent,df.pastor.honest.cath,df.pastor.honest.caris,df.pastor.honest.none,df.pastor.honest.atheist,df.pastor.honest.other)

# Generate Figure 

lwd<-1.5
adjust<-0.15

pdf(file='pastor_effects.pdf',width=6.5,height=6.5)
par(mar=c(6,9,3,2)+.01)
plot(diff.pastor.vote, 8:1+1.5*adjust, type='p',axes=F,frame.plot=T,xlab='',ylab='',pch=19,xlim=c(-1.5,1.5),ylim=c(1-1.5*adjust,8+1.5*adjust))
Axis(side=1,at=seq(-2,2,.5),labels=seq(-2,2,.5))
Axis(side=2,at=c(9:0),labels=c('','All','Evangelical','Pentecostal\nEvangelical','Catholic','Charismatic\nCatholic','No Organized\nReligion','Atheist/\nAgnostic','Other',''),las=1)
Axis(side=2,at=4.5,labels=c('Religion'),line=6,tick=F,font=2)
segments(diff.pastor.vote-qt(.975,df.pastor.vote)*se.pastor.vote,8:1+1.5*adjust,diff.pastor.vote+qt(.975,df.pastor.vote)*se.pastor.vote,8:1+1.5*adjust,lwd=lwd)
abline(v=0,lwd=lwd,lty=2)

segments(diff.pastor.comp-qt(.975,df.pastor.comp)*se.pastor.comp,8:1+.5*adjust,diff.pastor.comp+qt(.975,df.pastor.comp)*se.pastor.comp,8:1+.5*adjust,lwd=lwd)
points(diff.pastor.comp, 8:1+.5*adjust, pch=21, bg='white')

segments(diff.pastor.intel-qt(.975,df.pastor.intel)*se.pastor.intel,8:1-.5*adjust,diff.pastor.intel+qt(.975,df.pastor.intel)*se.pastor.intel,8:1-.5*adjust,lwd=lwd)
points(diff.pastor.intel, 8:1-.5*adjust, pch=8)

segments(diff.pastor.honest-qt(.975,df.pastor.honest)*se.pastor.honest,8:1-1.5*adjust,diff.pastor.honest+qt(.975,df.pastor.honest)*se.pastor.honest,8:1-1.5*adjust,lwd=lwd)
points(diff.pastor.honest, 8:1-1.5*adjust, pch=25, bg='white')

legend('bottomright',pch=c(19,21,8,25),legend=c('Vote','Competent','Intelligent','Honest'),title='DV (1-7)')

dev.off()

# ========
# Figure 2
# ========

reg.doctor.all<-lm(Q10_1~Q10_treat,data=survey)
diff.doctor.all<-coef(reg.doctor.all)['Q10_treat']
se.doctor.all<-sqrt(vcov(reg.doctor.all)['Q10_treat','Q10_treat'])
df.doctor.all<-reg.doctor.all$df.residual

reg.doctor.comp.all<-lm(Q10_2~Q10_treat,data=survey)
diff.doctor.comp.all<-coef(reg.doctor.comp.all)['Q10_treat']
se.doctor.comp.all<-sqrt(vcov(reg.doctor.comp.all)['Q10_treat','Q10_treat'])
df.doctor.comp.all<-reg.doctor.comp.all$df.residual

reg.doctor.intel.all<-lm(Q10_3~Q10_treat,data=survey)
diff.doctor.intel.all<-coef(reg.doctor.intel.all)['Q10_treat']
se.doctor.intel.all<-sqrt(vcov(reg.doctor.intel.all)['Q10_treat','Q10_treat'])
df.doctor.intel.all<-reg.doctor.intel.all$df.residual

reg.doctor.honest.all<-lm(Q10_4~Q10_treat,data=survey)
diff.doctor.honest.all<-coef(reg.doctor.honest.all)['Q10_treat']
se.doctor.honest.all<-sqrt(vcov(reg.doctor.honest.all)['Q10_treat','Q10_treat'])
df.doctor.honest.all<-reg.doctor.honest.all$df.residual

# Generate Figure

lwd<-1.5

pdf(file='doctor_effects.pdf',width=6.5,height=3.5)
par(mar=c(6,9,3,2)+.01)
plot(diff.doctor.all, 4, type='p',axes=F,frame.plot=T,xlab='',ylab='',pch=19,xlim=c(-.5,.5),ylim=c(.5,4.5))
Axis(side=1,at=seq(-.5,.5,.25),labels=seq(-.5,.5,.25))
Axis(side=2,at=c(4:1),labels=c('Vote','Competent','Intelligent','Honest'),las=1)
Axis(side=2,at=2.5,labels=c('DV (1-7)'),line=5.5,tick=F,font=2)
segments(diff.doctor.all-qt(.975,df.doctor.all)*se.doctor.all,4,diff.doctor.all+qt(.975,df.doctor.all)*se.doctor.all,4,lwd=lwd)
abline(v=0,lwd=lwd,lty=2)

segments(diff.doctor.comp.all-qt(.975,df.doctor.comp.all)*se.doctor.comp.all,3,diff.doctor.comp.all+qt(.975,df.doctor.comp.all)*se.doctor.comp.all,3,lwd=lwd)
points(diff.doctor.comp.all, 3, pch=21, bg='white')

segments(diff.doctor.intel.all-qt(.975,df.doctor.intel.all)*se.doctor.intel.all,2,diff.doctor.intel.all+qt(.975,df.doctor.intel.all)*se.doctor.intel.all,2,lwd=lwd)
points(diff.doctor.intel.all, 2, pch=8)

segments(diff.doctor.honest.all-qt(.975,df.doctor.honest.all)*se.doctor.honest.all,1,diff.doctor.honest.all+qt(.975,df.doctor.honest.all)*se.doctor.honest.all,1,lwd=lwd)
points(diff.doctor.honest.all, 1, pch=25, bg='white')

dev.off()

# =======
# Table 1
# =======

region.pop<-apply(region.pop[2:6],2,sum)
region.pct.pop<-100*prop.table(region.pop)
names(region.pct.pop)<-c('North','Northeast','Southeast','South','Center-West')

region.pct.sample<-100*prop.table(table(survey$region))
names(region.pct.sample)<-c('Center-West','Northeast','North','Southeast','South')

relig.pop$Catholic<-relig.pop$Católica.Apostólica.Romana + relig.pop$Católica.Apostólica.Brasileira
relig.pop$Evangelical<-relig.pop$Evangélicas
relig.pop$None<-relig.pop$'Sem.religião...Sem.religião'
relig.pop$'Atheist/Agnostic'<-relig.pop$'Sem.religião...Ateu'+relig.pop$'Sem.religião...Agnóstico'
relig.pop$Other<-relig.pop$Total - (relig.pop$Catholic + relig.pop$Evangelical + relig.pop$None + relig.pop$'Atheist/Agnostic')
relig.pop<-apply(relig.pop[,c('Catholic','Evangelical','None','Atheist/Agnostic','Other')],2,sum)
relig.pct.pop<-100*prop.table(relig.pop)

relig.pct.sample<-100*prop.table(table(survey$Q16))
names(relig.pct.sample)<-c('Catholic','Evangelical','None','Atheist/Agnostic','Other')

age.pop$pct<-100*prop.table(age.pop$total)
age.pop$cum.pct<-cumsum(age.pop$pct)
age.med.pop<-age.pop$age[which(age.pop$cum.pct==min(age.pop$cum.pct[age.pop$cum.pct>50]))]

age.med.sample<-median(survey$Q28,na.rm=T)

sex.pop<-apply(age.pop[,c('men','women')],2,sum)
sex.pct.pop<-100*sex.pop/sum(sex.pop)
names(sex.pct.pop)<-c('Men','Women')

sex.pct.sample<-100*prop.table(table(survey$Q27))
names(sex.pct.sample)<-c('Men','Women')

race.pop<-apply(race.pop[,3:7],2,sum)
race.pct.pop<-100*race.pop/sum(race.pop)
names(race.pct.pop)<-c('White','Black','Asian','Brown','Indigenous')

race.pct.sample<-100*prop.table(table(survey$Q25))
names(race.pct.sample)<-c('White','Black','Brown','Asian','Indigenous')

income.pop<-c(sum(income.pop[c('00 SM','00-01 SM','01-02 SM')]),sum(income.pop[c('02-03 SM','03-05 SM')]),income.pop['05+ SM'])
income.pct.pop<-100*income.pop/sum(income.pop)
names(income.pct.pop)<-c('0-2 MW','2-5 MW','5+ MW')

income.sample<-c(sum(survey$Q29==1,na.rm=T),sum(survey$Q29==2,na.rm=T),sum(survey$Q29>2,na.rm=T))
income.pct.sample<-100*income.sample/sum(income.sample)
names(income.pct.sample)<-c('0-2 MW','2-5 MW','5+ MW')

muni.pop<-data.frame(muni=muni.pop$X,codeibge=muni.pop$codeibge,Total=muni.pop$Total,Over18=apply(muni.pop[,grep('anos',names(muni.pop))],1,sum,na.rm=T))
muni.pop<-muni.pop[order(muni.pop$Over18),]
muni.pop$pct<-100*muni.pop$Over18/sum(muni.pop$Over18)
muni.pop$cum.pct<-cumsum(muni.pop$pct)
muni.med.pop<-muni.pop$Total[which(muni.pop$cum.pct==min(muni.pop$cum.pct[muni.pop$cum.pct>50]))]

muni.med.sample<-median(survey$pop.muni,na.rm=T)

muni.pop<-merge(muni.pop,munis12[,c('codeibge','pastors','doctors')],all.x=T)
muni.pop<-muni.pop[order(muni.pop$pastors),]
muni.pop$cum.pct.pastors<-cumsum(muni.pop$pct)
pastors.med.pop<-muni.pop$pastors[which(muni.pop$cum.pct.pastors==min(muni.pop$cum.pct.pastors[muni.pop$cum.pct.pastors>50]))]

pastors.med.sample<-median(survey$pastors,na.rm=T)

muni.pop<-muni.pop[order(muni.pop$doctors),]
muni.pop$cum.pct.doctors<-cumsum(muni.pop$pct)
doctors.med.pop<-muni.pop$doctors[which(muni.pop$cum.pct.doctors==min(muni.pop$cum.pct.doctors[muni.pop$cum.pct.doctors>50]))]

doctors.med.sample<-median(survey$doctors,na.rm=T)

sample.stats.census<-data.frame(t(round(c(muni.med.sample,pastors.med.sample,doctors.med.sample,region.pct.sample,relig.pct.sample,race.pct.sample,income.pct.sample,age.med.sample,sex.pct.sample),1)))
pop.stats.census<-data.frame(t(round(c(muni.med.pop,pastors.med.pop,doctors.med.pop,region.pct.pop,relig.pct.pop,race.pct.pop,income.pct.pop,age.med.pop,sex.pct.pop),1)))
rep.table.census<-data.frame(t(rbind(sample.stats.census,pop.stats.census)))
rep.table.census<-rep.table.census[-1*which(rownames(rep.table.census)=='Women'),]
rep.table.census<-apply(rep.table.census,2,prettyNum,big.mark=',')

rownames(rep.table.census)[c(1:4,11:12,19:23)]<-c('Median Population',"Median ``Pastor'' Candidates","Median ``Doctor'' Candidates",'Center-West','No Organized Religion','Atheist/Agnostic','0--2 $\\times$ Min.\\ Wage','2--5 $\\times$ Min.\\ Wage','5+ $\\times$ Min.\\ Wage','Median Age','Male')

bottom.note.census<-c("Census data are for residents 18 and older. Municipality figures are those associated with the median individual. Non-median figures are percentages.")
rep.table.census.latex<-latex(rep.table.census,file='rep_table_census.tex',caption='Online Sample vs. 2010 Census',rowlabel='',colheads=c('Online Sample','Census'),collabel.just=rep('r',2),col.just = rep('r',2),insert.bottom=bottom.note.census,rgroup = c('Municipality','Region','Religion','Race','Household Income','Other'), n.rgroup = c(3,5,5,5,3,2), booktabs = F, ctable = T, where = "htp")

# =======
# Table 2
# =======

# Download the Brazil 2012 AmericasBarometer survey (Stata version) from http://www.vanderbilt.edu/lapop/free-access.php and load it, modifying the code below as necessary for the filename and location. If the filename differs from that below, you may be working with a newer version of the file, which could potentially affect the results or require changes to the code.

lapop12<-read.dta('54861031Brazil LAPOP AmericasBarometer 2012 Rev1_W.dta')

# Restrict to registered voters (everyone is over 18)
lapop12<-lapop12[unclass(lapop12$vb1)==1,]

lapop12$church<-lapop12$q5a[,drop=T]
church.pct.pop<-100*prop.table(table(lapop12$church))

church.pct.sample<-100*prop.table(table(survey$Q19))
names(church.pct.sample)<-names(church.pct.pop)

lapop12$noparty<-unclass(lapop12$vb10) == 2
lapop12$PT<-unclass(lapop12$vb11) %in% 18
lapop12$PT[is.na(lapop12$vb10)]<-NA
lapop12$PSDB<-unclass(lapop12$vb11) %in% 20
lapop12$PSDB[is.na(lapop12$vb10)]<-NA
lapop12$PMDB<-unclass(lapop12$vb11) %in% 19
lapop12$PMDB[is.na(lapop12$vb10)]<-NA
lapop12$otherparty<-unclass(lapop12$vb11) %in% c(21:31)
lapop12$otherparty[is.na(lapop12$vb10)]<-NA

party.pct.pop<-100*apply(lapop12[,c('noparty','PT','PSDB','PMDB','otherparty')],2,mean,na.rm=T)
party.pct.sample<-100*apply(survey[,c('noparty','PT','PSDB','PMDB','otherparty')],2,mean,na.rm=T)

lapop12$novote<-unclass(lapop12$vb2)==2
lapop12$dilma<-unclass(lapop12$vb3) %in% c(1501,1)
lapop12$dilma[is.na(lapop12$vb2)|unclass(lapop12$vb2)==1&is.na(lapop12$vb3)]<-NA
lapop12$serra<-unclass(lapop12$vb3) %in% c(1502,2)
lapop12$serra[is.na(lapop12$vb2)|unclass(lapop12$vb2)==1&is.na(lapop12$vb3)]<-NA
lapop12$marina<-unclass(lapop12$vb3) %in% c(1503,3)
lapop12$marina[is.na(lapop12$vb2)|unclass(lapop12$vb2)==1&is.na(lapop12$vb3)]<-NA
lapop12$blank<-unclass(lapop12$vb3) %in% c(0,10)
lapop12$blank[is.na(lapop12$vb2)|unclass(lapop12$vb2)==1&is.na(lapop12$vb3)]<-NA

vote.pct.pop<-100*apply(lapop12[,c('novote','dilma','serra','marina','blank')],2,mean,na.rm=T)

survey$novote<-survey$Q23==6
survey$novote[survey$Q23==8]<-NA
survey$dilma<-survey$Q23==1
survey$dilma[survey$Q23==8]<-NA
survey$serra<-survey$Q23==2
survey$serra[survey$Q23==8]<-NA
survey$marina<-survey$Q23==3
survey$marina[survey$Q23==8]<-NA
survey$blank<-survey$Q23==7
survey$blank[survey$Q23==8]<-NA

vote.pct.sample<-100*apply(survey[,c('novote','dilma','serra','marina','blank')],2,mean,na.rm=T)

registered<-sum(as.numeric(gsub('\\.','',turnout2010$Qt.Aptos)))
abstained<-sum(as.numeric(gsub('\\.','',turnout2010$Abstenção)))
blank.null<-sum(as.numeric(gsub('\\.','',vote2010$Qt.VotosBrancos)))+sum(as.numeric(gsub('\\.','',vote2010$Qt.VotosNulos)))
dilma<-as.numeric(gsub('\\.','',pres2010$VotaçãoNominal[pres2010$Candidato=='DILMA VANA ROUSSEFF']))
serra<-as.numeric(gsub('\\.','',pres2010$VotaçãoNominal[pres2010$Candidato=='JOSÉ SERRA']))
marina<-as.numeric(gsub('\\.','',pres2010$VotaçãoNominal[pres2010$Candidato=='MARIA OSMARINA MARINA DA SILVA VAZ DE LIMA']))

sample.stats.lapop<-data.frame(t(round(c(church.pct.sample,party.pct.sample,vote.pct.sample),1)))
pop.stats.lapop<-data.frame(t(round(c(church.pct.pop,party.pct.pop,vote.pct.pop),1)))
rep.table.lapop<-data.frame(t(rbind(sample.stats.lapop,pop.stats.lapop)))
vote.stats.lapop<-c(rep(NA,10),round(100*c(abstained,dilma,serra,marina,blank.null)/registered,1))
rep.table.lapop<-cbind(rep.table.lapop, vote.stats.lapop)
rep.table.lapop<-apply(rep.table.lapop,2,function(x) ifelse(is.na(x),'',prettyNum(x,big.mark=',')))

rownames(rep.table.lapop)[c(1:6,10:15)]<-c('1+ Times/Week','1 Time/Week','1 Time/Month','1--2 Times/Year','Never/Almost Never','None','Other Party','Did Not Vote','Dilma','Serra','Marina','Blank/Null')

bottom.note.lapop<-c("All figures expressed as percentages of registered voters.")

rep.table.lapop.latex<-latex(rep.table.lapop,file='rep_table_lapop.tex',caption="Online Sample vs. 2012 AmericasBarometer and Electoral Results",rowlabel='',colheads=c('Online Sample','AmericasBarometer','Electoral Results'),collabel.just=rep('r',3),col.just = rep('r',3),insert.bottom=bottom.note.lapop,rgroup = c('Church Attendance','Party ID','2010 Presidential Vote'), n.rgroup = c(5,5,5), booktabs = F, ctable = T, where = "htp")

# =======
# Table 3
# =======

reg.real.pastor.vote<-lm(Q9_1~Q9_treat*pastor.treat.type,data=survey[survey$pastor.treat.type %in% c('fake candidate','real candidate'),]) # Vote
reg.real.pastor.comp<-lm(Q9_2~Q9_treat*pastor.treat.type,data=survey[survey$pastor.treat.type %in% c('fake candidate','real candidate'),]) # Competent
reg.real.pastor.intel<-lm(Q9_3~Q9_treat*pastor.treat.type,data=survey[survey$pastor.treat.type %in% c('fake candidate','real candidate'),]) # Intelligent
reg.real.pastor.honest<-lm(Q9_4~Q9_treat*pastor.treat.type,data=survey[survey$pastor.treat.type %in% c('fake candidate','real candidate'),]) # Honest

reg.real.doctor.vote<-lm(Q10_1~Q10_treat*doctor.treat.type,data=survey[survey$doctor.treat.type %in% c('fake candidate','real candidate'),]) # Vote
reg.real.doctor.comp<-lm(Q10_2~Q10_treat*doctor.treat.type,data=survey[survey$doctor.treat.type %in% c('fake candidate','real candidate'),]) # Competent
reg.real.doctor.intel<-lm(Q10_3~Q10_treat*doctor.treat.type,data=survey[survey$doctor.treat.type %in% c('fake candidate','real candidate'),]) # Intelligent
reg.real.doctor.honest<-lm(Q10_4~Q10_treat*doctor.treat.type,data=survey[survey$doctor.treat.type %in% c('fake candidate','real candidate'),]) # Honest

se.real.pastor.vote<-paste('(',round(coef(summary(reg.real.pastor.vote))[,2],2),')',sep='')
se.real.pastor.comp<-paste('(',round(coef(summary(reg.real.pastor.comp))[,2],2),')',sep='')
se.real.pastor.intel<-paste('(',round(coef(summary(reg.real.pastor.intel))[,2],2),')',sep='')
se.real.pastor.honest<-paste('(',round(coef(summary(reg.real.pastor.honest))[,2],2),')',sep='')
se.real.doctor.vote<-paste('(',round(coef(summary(reg.real.doctor.vote))[,2],2),')',sep='')
se.real.doctor.comp<-paste('(',round(coef(summary(reg.real.doctor.comp))[,2],2),')',sep='')
se.real.doctor.intel<-paste('(',round(coef(summary(reg.real.doctor.intel))[,2],2),')',sep='')
se.real.doctor.honest<-paste('(',round(coef(summary(reg.real.doctor.honest))[,2],2),')',sep='')

spacer.real.pastor.vote<-ifelse(round(coef(summary(reg.real.pastor.vote))[,1],2)<0,'','\\ ')
spacer.real.pastor.comp<-ifelse(round(coef(summary(reg.real.pastor.comp))[,1],2)<0,'','\\ ')
spacer.real.pastor.intel<-ifelse(round(coef(summary(reg.real.pastor.intel))[,1],2)<0,'','\\ ')
spacer.real.pastor.honest<-ifelse(round(coef(summary(reg.real.pastor.honest))[,1],2)<0,'','\\ ')
spacer.real.doctor.vote<-ifelse(round(coef(summary(reg.real.doctor.vote))[,1],2)<0,'','\\ ')
spacer.real.doctor.comp<-ifelse(round(coef(summary(reg.real.doctor.comp))[,1],2)<0,'','\\ ')
spacer.real.doctor.intel<-ifelse(round(coef(summary(reg.real.doctor.intel))[,1],2)<0,'','\\ ')
spacer.real.doctor.honest<-ifelse(round(coef(summary(reg.real.doctor.honest))[,1],2)<0,'','\\ ')

est.real.pastor.vote<-paste(spacer.real.pastor.vote,round(coef(summary(reg.real.pastor.vote))[,1],2),sep='')
est.real.pastor.comp<-paste(spacer.real.pastor.comp,round(coef(summary(reg.real.pastor.comp))[,1],2),sep='')
est.real.pastor.intel<-paste(spacer.real.pastor.intel,round(coef(summary(reg.real.pastor.intel))[,1],2),sep='')
est.real.pastor.honest<-paste(spacer.real.pastor.honest,round(coef(summary(reg.real.pastor.honest))[,1],2),sep='')
est.real.doctor.vote<-paste(spacer.real.doctor.vote,round(coef(summary(reg.real.doctor.vote))[,1],2),sep='')
est.real.doctor.comp<-paste(spacer.real.doctor.comp,round(coef(summary(reg.real.doctor.comp))[,1],2),sep='')
est.real.doctor.intel<-paste(spacer.real.doctor.intel,round(coef(summary(reg.real.doctor.intel))[,1],2),sep='')
est.real.doctor.honest<-paste(spacer.real.doctor.honest,round(coef(summary(reg.real.doctor.honest))[,1],2),sep='')

real.pastor.vote<-c(as.vector(rbind(est.real.pastor.vote,se.real.pastor.vote)),reg.real.pastor.vote$rank+reg.real.pastor.vote$df.residual)
real.pastor.comp<-c(as.vector(rbind(est.real.pastor.comp,se.real.pastor.comp)),reg.real.pastor.comp$rank+reg.real.pastor.comp$df.residual)
real.pastor.intel<-c(as.vector(rbind(est.real.pastor.intel,se.real.pastor.intel)),reg.real.pastor.intel$rank+reg.real.pastor.intel$df.residual)
real.pastor.honest<-c(as.vector(rbind(est.real.pastor.honest,se.real.pastor.honest)),reg.real.pastor.honest$rank+reg.real.pastor.honest$df.residual)
real.doctor.vote<-c(as.vector(rbind(est.real.doctor.vote,se.real.doctor.vote)),reg.real.doctor.vote$rank+reg.real.doctor.vote$df.residual)
real.doctor.comp<-c(as.vector(rbind(est.real.doctor.comp,se.real.doctor.comp)),reg.real.doctor.comp$rank+reg.real.doctor.comp$df.residual)
real.doctor.intel<-c(as.vector(rbind(est.real.doctor.intel,se.real.doctor.intel)),reg.real.doctor.intel$rank+reg.real.doctor.intel$df.residual)
real.doctor.honest<-c(as.vector(rbind(est.real.doctor.honest,se.real.doctor.honest)),reg.real.doctor.honest$rank+reg.real.doctor.honest$df.residual)

# Generate Table

results.table.real<-data.frame(real.pastor.vote,real.pastor.comp,real.pastor.intel,real.pastor.honest,real.doctor.vote,real.doctor.comp,real.doctor.intel,real.doctor.honest)
bottom.note<-paste("NOTE: Entries are OLS regression coefficients with estimated standard errors in parentheses. Respondents from municipalities with no real ``pastor'' or ``doctor'' candidates are excluded from the corresponding regressions.")

results.table.real.latex<-latex(results.table.real,file='results_table_real.tex',collabel.just=rep('l',8),col.just = rep('l',8),rowname=c('Intercept','','Treatment','','Real Candidate','','Treatment $\\times$ Real Candidate','','\\textit{N}'),rowlabel='',colheads=c('Pastor:\nVote','Pastor:\nCompetent','Pastor:\nIntelligent','Pastor:\nHonest','Doctor:\nVote','Doctor:\nCompetent','Doctor:\nIntelligent','Doctor:\nHonest'),extracolsize='normalsize',caption = 'Effect of Real versus Fictional Candidate Treatments',insert.bottom=bottom.note,booktabs = F, ctable = T, where = "htp",cgroup='DV',n.cgroup=8,landscape=T)

# =======
# Table 4
# =======

reg.screen.pastor.vote<-lm(Q9_1~Q9_treat*screener,data=survey)
reg.screen.pastor.comp<-lm(Q9_2~Q9_treat*screener,data=survey)
reg.screen.pastor.intel<-lm(Q9_3~Q9_treat*screener,data=survey)
reg.screen.pastor.honest<-lm(Q9_4~Q9_treat*screener,data=survey)
reg.screen.doctor.vote<-lm(Q10_1~Q10_treat*screener,data=survey)
reg.screen.doctor.comp<-lm(Q10_2~Q10_treat*screener,data=survey)
reg.screen.doctor.intel<-lm(Q10_3~Q10_treat*screener,data=survey)
reg.screen.doctor.honest<-lm(Q10_4~Q10_treat*screener,data=survey)

se.screen.pastor.vote<-paste('(',round(coef(summary(reg.screen.pastor.vote))[,2],2),')',sep='')
se.screen.pastor.comp<-paste('(',round(coef(summary(reg.screen.pastor.comp))[,2],2),')',sep='')
se.screen.pastor.intel<-paste('(',round(coef(summary(reg.screen.pastor.intel))[,2],2),')',sep='')
se.screen.pastor.honest<-paste('(',round(coef(summary(reg.screen.pastor.honest))[,2],2),')',sep='')
se.screen.doctor.vote<-paste('(',round(coef(summary(reg.screen.doctor.vote))[,2],2),')',sep='')
se.screen.doctor.comp<-paste('(',round(coef(summary(reg.screen.doctor.comp))[,2],2),')',sep='')
se.screen.doctor.intel<-paste('(',round(coef(summary(reg.screen.doctor.intel))[,2],2),')',sep='')
se.screen.doctor.honest<-paste('(',round(coef(summary(reg.screen.doctor.honest))[,2],2),')',sep='')

spacer.screen.pastor.vote<-ifelse(round(coef(summary(reg.screen.pastor.vote))[,1],2)<0,'','\\ ')
spacer.screen.pastor.comp<-ifelse(round(coef(summary(reg.screen.pastor.comp))[,1],2)<0,'','\\ ')
spacer.screen.pastor.intel<-ifelse(round(coef(summary(reg.screen.pastor.intel))[,1],2)<0,'','\\ ')
spacer.screen.pastor.honest<-ifelse(round(coef(summary(reg.screen.pastor.honest))[,1],2)<0,'','\\ ')
spacer.screen.doctor.vote<-ifelse(round(coef(summary(reg.screen.doctor.vote))[,1],2)<0,'','\\ ')
spacer.screen.doctor.comp<-ifelse(round(coef(summary(reg.screen.doctor.comp))[,1],2)<0,'','\\ ')
spacer.screen.doctor.intel<-ifelse(round(coef(summary(reg.screen.doctor.intel))[,1],2)<0,'','\\ ')
spacer.screen.doctor.honest<-ifelse(round(coef(summary(reg.screen.doctor.honest))[,1],2)<0,'','\\ ')

est.screen.pastor.vote<-paste(spacer.screen.pastor.vote,round(coef(summary(reg.screen.pastor.vote))[,1],2),sep='')
est.screen.pastor.comp<-paste(spacer.screen.pastor.comp,round(coef(summary(reg.screen.pastor.comp))[,1],2),sep='')
est.screen.pastor.intel<-paste(spacer.screen.pastor.intel,round(coef(summary(reg.screen.pastor.intel))[,1],2),sep='')
est.screen.pastor.honest<-paste(spacer.screen.pastor.honest,round(coef(summary(reg.screen.pastor.honest))[,1],2),sep='')
est.screen.doctor.vote<-paste(spacer.screen.doctor.vote,round(coef(summary(reg.screen.doctor.vote))[,1],2),sep='')
est.screen.doctor.comp<-paste(spacer.screen.doctor.comp,round(coef(summary(reg.screen.doctor.comp))[,1],2),sep='')
est.screen.doctor.intel<-paste(spacer.screen.doctor.intel,round(coef(summary(reg.screen.doctor.intel))[,1],2),sep='')
est.screen.doctor.honest<-paste(spacer.screen.doctor.honest,round(coef(summary(reg.screen.doctor.honest))[,1],2),sep='')

screen.pastor.vote<-c(as.vector(rbind(est.screen.pastor.vote,se.screen.pastor.vote)),reg.screen.pastor.vote$rank+reg.screen.pastor.vote$df.residual)
screen.pastor.comp<-c(as.vector(rbind(est.screen.pastor.comp,se.screen.pastor.comp)),reg.screen.pastor.comp$rank+reg.screen.pastor.comp$df.residual)
screen.pastor.intel<-c(as.vector(rbind(est.screen.pastor.intel,se.screen.pastor.intel)),reg.screen.pastor.intel$rank+reg.screen.pastor.intel$df.residual)
screen.pastor.honest<-c(as.vector(rbind(est.screen.pastor.honest,se.screen.pastor.honest)),reg.screen.pastor.honest$rank+reg.screen.pastor.honest$df.residual)
screen.doctor.vote<-c(as.vector(rbind(est.screen.doctor.vote,se.screen.doctor.vote)),reg.screen.doctor.vote$rank+reg.screen.doctor.vote$df.residual)
screen.doctor.comp<-c(as.vector(rbind(est.screen.doctor.comp,se.screen.doctor.comp)),reg.screen.doctor.comp$rank+reg.screen.doctor.comp$df.residual)
screen.doctor.intel<-c(as.vector(rbind(est.screen.doctor.intel,se.screen.doctor.intel)),reg.screen.doctor.intel$rank+reg.screen.doctor.intel$df.residual)
screen.doctor.honest<-c(as.vector(rbind(est.screen.doctor.honest,se.screen.doctor.honest)),reg.screen.doctor.honest$rank+reg.screen.doctor.honest$df.residual)

# Generate Table

results.table.screen<-data.frame(screen.pastor.vote,screen.pastor.comp,screen.pastor.intel,screen.pastor.honest,screen.doctor.vote,screen.doctor.comp,screen.doctor.intel,screen.doctor.honest)
bottom.note<-paste("NOTE: Entries are OLS regression coefficients with estimated standard errors in parentheses.")

results.table.screen.latex<-latex(results.table.screen,file='results_table_screen.tex',collabel.just=rep('l',8),col.just = rep('l',8),rowname=c('Intercept','','Treatment','','One Screener','','Both Screeners','','Treatment $\\times$ One Screener','','Treatment $\\times$ Both Screeners','','\\textit{N}'),rowlabel='',colheads=c('Pastor:\nVote','Pastor:\nCompetent','Pastor:\nIntelligent','Pastor:\nHonest','Doctor:\nVote','Doctor:\nCompetent','Doctor:\nIntelligent','Doctor:\nHonest'),extracolsize='normalsize',caption = 'Variation in Treatment Effects by Screener Passage',insert.bottom=bottom.note,booktabs = F, ctable = T, where = "htp",cgroup='DV',n.cgroup=8,landscape=T)

# =======
# Table 5
# =======

bal.data.pastor<-survey[,c('Q9_treat','log.pop.muni','evangelical.muni','catholic.muni','lng','lat','pastors','north','northeast','centerwest','southeast','south','evangelical','catholic','none','atheist','other','pentecostal','charismatic','church','white','black','brown','yellow','indigenous','noparty','PT','PSDB','PMDB','otherparty','Q29','Q6','Q28','male')]
pastor.mean.treat<-apply(bal.data.pastor[which(survey$Q9_treat==1),-1],2,mean,na.rm=T)
pastor.mean.control<-apply(bal.data.pastor[which(survey$Q9_treat==0),-1],2,mean,na.rm=T)
pastor.sd<-apply(bal.data.pastor[,-1],2,sd,na.rm=T)
pastor.stddiff<- (pastor.mean.treat - pastor.mean.control)/pastor.sd
pastor.ttest<-apply(bal.data.pastor[,-1],2,function(x) t.test(x[which(survey$Q9_treat==1)],x[which(survey$Q9_treat==0)])$p.value)
pastor.varrat<-apply(bal.data.pastor[,-1],2,function(x) var(x[which(survey$Q9_treat==1)],na.rm=T)/var(x[which(survey$Q9_treat==0)],na.rm=T))
pastor.kstest<- apply(bal.data.pastor[,-1],2,function(x) ifelse(length(table(x))==2,NA,ks.boot(Tr=x[which(survey$Q9_treat==1)],Co=x[which(survey$Q9_treat==0)],nboots=1000)$ks.boot.pvalue))

# Generate Table

bal.table.pastor<-round(data.frame(pastor.mean.treat,pastor.mean.control,pastor.stddiff,pastor.varrat,pastor.ttest,pastor.kstest),2)
names(bal.table.pastor)<-c('Treated','Control','Std.\\ Diff.','Var.\\ Rat.','t-test','KS-test')
rownames(bal.table.pastor)<-c('Log Population','Pct.\\ Evangelical','Pct.\\ Catholic','Longitude','Latitude','Pastor Candidates','North','Northeast','Center-West','Southeast','South','Evangelical','Catholic','No Organized Religion','Atheist/Agnostic','Other','Pentecostal','Charismatic','Church Attendance (1--5)','White','Black','Brown','Asian','Indigenous','None','PT','PSDB','PMDB','Other Party','Income (1--8)','Campaign Interest (1--7)','Age','Male')
bottom.note<-c("`Treated' and `Control' give mean values; `Std.\\ Diff.' is their difference divided by the pooled standard deviation. `Var.\\ Rat.' is the ratio of treatment to control group variance. `t-test' and `KS-test' give two-sided p-values (bootstrapped for KS).") 

bal.table.pastor.latex<-latex(bal.table.pastor,file='bal_table_pastor.tex',rowlabel='',collabel.just=rep('r',6),col.just = rep('r',6), caption = "Covariate Balance for ``Pastor'' Treatment",insert.bottom=bottom.note,rgroup = c('Municipality','Region','Religion','Race','Party ID','Other'), n.rgroup = c(6,5,8,5,5,4), booktabs = F, ctable = T, where = "htp",na.blank=T)

# =======
# Table 6
# =======

bal.data.doctor<-survey[,c('Q10_treat','log.pop.muni','evangelical.muni','catholic.muni','lng','lat','doctors','north','northeast','centerwest','southeast','south','evangelical','catholic','none','atheist','other','pentecostal','charismatic','church','white','black','brown','yellow','indigenous','noparty','PT','PSDB','PMDB','otherparty','Q29','Q6','Q28','male')]
doctor.mean.treat<-apply(bal.data.doctor[which(survey$Q10_treat==1),-1],2,mean,na.rm=T)
doctor.mean.control<-apply(bal.data.doctor[which(survey$Q10_treat==0),-1],2,mean,na.rm=T)
doctor.sd<-apply(bal.data.doctor[,-1],2,sd,na.rm=T)
doctor.stddiff<- (doctor.mean.treat - doctor.mean.control)/doctor.sd
doctor.ttest<-apply(bal.data.doctor[,-1],2,function(x) t.test(x[which(survey$Q10_treat==1)],x[which(survey$Q10_treat==0)])$p.value)
doctor.varrat<-apply(bal.data.doctor[,-1],2,function(x) var(x[which(survey$Q10_treat==1)],na.rm=T)/var(x[which(survey$Q10_treat==0)],na.rm=T))
doctor.kstest<- apply(bal.data.doctor[,-1],2,function(x) ifelse(length(table(x))==2,NA,ks.boot(Tr=x[which(survey$Q10_treat==1)],Co=x[which(survey$Q10_treat==0)],nboots=1000)$ks.boot.pvalue))

# Generate Table

bal.table.doctor<-round(data.frame(doctor.mean.treat,doctor.mean.control,doctor.stddiff,doctor.varrat,doctor.ttest,doctor.kstest),2)
names(bal.table.doctor)<-c('Treated','Control','Std.\\ Diff.','Var.\\ Rat.','t-test','KS-test')
rownames(bal.table.doctor)<-c('Log Population','Pct.\\ Evangelical','Pct.\\ Catholic','Longitude','Latitude','Doctor Candidates','North','Northeast','Center-West','Southeast','South','Evangelical','Catholic','No Organized Religion','Atheist/Agnostic','Other','Pentecostal','Charismatic','Church Attendance (1--5)','White','Black','Brown','Asian','Indigenous','None','PT','PSDB','PMDB','Other Party','Income (1--8)','Campaign Interest (1--7)','Age','Male')
bottom.note<-c("`Treated' and `Control' give mean values; `Std.\\ Diff.' is their difference divided by the pooled standard deviation. `Var.\\ Rat.' is the ratio of treatment to control group variance. `t-test' and `KS-test' give two-sided p-values (bootstrapped for KS).") 

bal.table.doctor.latex<-latex(bal.table.doctor,file='bal_table_doctor.tex',rowlabel='',collabel.just=rep('r',6),col.just = rep('r',6), caption = "Covariate Balance for ``Doctor'' Treatment",insert.bottom=bottom.note,rgroup = c('Municipality','Region','Religion','Race','Party ID','Other'), n.rgroup = c(6,5,8,5,5,4), booktabs = F, ctable = T, where = "htp",na.blank=T)

# =======
# Table 7
# =======

reg.control.pastor.vote<-lm(Q9_1~Q9_treat+catholic.muni+charismatic,data=survey)
reg.control.pastor.comp<-lm(Q9_2~Q9_treat+catholic.muni+charismatic,data=survey)
reg.control.pastor.intel<-lm(Q9_3~Q9_treat+catholic.muni+charismatic,data=survey)
reg.control.pastor.honest<-lm(Q9_4~Q9_treat+catholic.muni+charismatic,data=survey)

se.control.pastor.vote<-paste('(',round(coef(summary(reg.control.pastor.vote))[,2],2),')',sep='')
se.control.pastor.comp<-paste('(',round(coef(summary(reg.control.pastor.comp))[,2],2),')',sep='')
se.control.pastor.intel<-paste('(',round(coef(summary(reg.control.pastor.intel))[,2],2),')',sep='')
se.control.pastor.honest<-paste('(',round(coef(summary(reg.control.pastor.honest))[,2],2),')',sep='')

se.nocontrol.pastor.vote<-c(paste('(',round(coef(summary(reg.pastor.vote.all))[,2],2),')',sep=''),'','')
se.nocontrol.pastor.comp<-c(paste('(',round(coef(summary(reg.pastor.comp.all))[,2],2),')',sep=''),'','')
se.nocontrol.pastor.intel<-c(paste('(',round(coef(summary(reg.pastor.intel.all))[,2],2),')',sep=''),'','')
se.nocontrol.pastor.honest<-c(paste('(',round(coef(summary(reg.pastor.honest.all))[,2],2),')',sep=''),'','')

spacer.control.pastor.vote<-ifelse(round(coef(summary(reg.control.pastor.vote))[,1],2)<0,'','\\ ')
spacer.control.pastor.comp<-ifelse(round(coef(summary(reg.control.pastor.comp))[,1],2)<0,'','\\ ')
spacer.control.pastor.intel<-ifelse(round(coef(summary(reg.control.pastor.intel))[,1],2)<0,'','\\ ')
spacer.control.pastor.honest<-ifelse(round(coef(summary(reg.control.pastor.honest))[,1],2)<0,'','\\ ')

spacer.nocontrol.pastor.vote<-ifelse(round(coef(summary(reg.pastor.vote.all))[,1],2)<0,'','\\ ')
spacer.nocontrol.pastor.comp<-ifelse(round(coef(summary(reg.pastor.comp.all))[,1],2)<0,'','\\ ')
spacer.nocontrol.pastor.intel<-ifelse(round(coef(summary(reg.pastor.intel.all))[,1],2)<0,'','\\ ')
spacer.nocontrol.pastor.honest<-ifelse(round(coef(summary(reg.pastor.honest.all))[,1],2)<0,'','\\ ')

est.control.pastor.vote<-paste(spacer.control.pastor.vote,round(coef(summary(reg.control.pastor.vote))[,1],2),sep='')
est.control.pastor.comp<-paste(spacer.control.pastor.comp,round(coef(summary(reg.control.pastor.comp))[,1],2),sep='')
est.control.pastor.intel<-paste(spacer.control.pastor.intel,round(coef(summary(reg.control.pastor.intel))[,1],2),sep='')
est.control.pastor.honest<-paste(spacer.control.pastor.honest,round(coef(summary(reg.control.pastor.honest))[,1],2),sep='')

est.nocontrol.pastor.vote<-c(paste(spacer.nocontrol.pastor.vote,round(coef(summary(reg.pastor.vote.all))[,1],2),sep=''),'','')
est.nocontrol.pastor.comp<-c(paste(spacer.nocontrol.pastor.comp,round(coef(summary(reg.pastor.comp.all))[,1],2),sep=''),'','')
est.nocontrol.pastor.intel<-c(paste(spacer.nocontrol.pastor.intel,round(coef(summary(reg.pastor.intel.all))[,1],2),sep=''),'','')
est.nocontrol.pastor.honest<-c(paste(spacer.nocontrol.pastor.honest,round(coef(summary(reg.pastor.honest.all))[,1],2),sep=''),'','')

control.pastor.vote<-c(as.vector(rbind(est.control.pastor.vote,se.control.pastor.vote)),reg.control.pastor.vote$rank+reg.control.pastor.vote$df.residual)
control.pastor.comp<-c(as.vector(rbind(est.control.pastor.comp,se.control.pastor.comp)),reg.control.pastor.comp$rank+reg.control.pastor.comp$df.residual)
control.pastor.intel<-c(as.vector(rbind(est.control.pastor.intel,se.control.pastor.intel)),reg.control.pastor.intel$rank+reg.control.pastor.intel$df.residual)
control.pastor.honest<-c(as.vector(rbind(est.control.pastor.honest,se.control.pastor.honest)),reg.control.pastor.honest$rank+reg.control.pastor.honest$df.residual)

nocontrol.pastor.vote<-c(as.vector(rbind(est.nocontrol.pastor.vote,se.nocontrol.pastor.vote)),reg.pastor.vote.all$rank+reg.pastor.vote.all$df.residual)
nocontrol.pastor.comp<-c(as.vector(rbind(est.nocontrol.pastor.comp,se.nocontrol.pastor.comp)),reg.pastor.comp.all$rank+reg.pastor.comp.all$df.residual)
nocontrol.pastor.intel<-c(as.vector(rbind(est.nocontrol.pastor.intel,se.nocontrol.pastor.intel)),reg.pastor.intel.all$rank+reg.pastor.intel.all$df.residual)
nocontrol.pastor.honest<-c(as.vector(rbind(est.nocontrol.pastor.honest,se.nocontrol.pastor.honest)),reg.pastor.honest.all$rank+reg.pastor.honest.all$df.residual)

results.table.pastor.control<-data.frame(nocontrol.pastor.vote, control.pastor.vote, nocontrol.pastor.comp, control.pastor.comp, nocontrol.pastor.intel, control.pastor.intel, nocontrol.pastor.honest, control.pastor.honest)

bottom.note<-paste("NOTE: Entries are OLS regression coefficients with estimated standard errors in parentheses.")

results.table.pastor.control.latex<-latex(results.table.pastor.control,file='results_table_pastor_control.tex',collabel.just=rep('l',8),col.just = rep('l',8),rowname=c('Intercept','','Treatment','','Mun. Pct. Catholic','','Charismatic Catholic','','\\textit{N}'),rowlabel='',colheads=c('Vote','Vote','Competent','Competent','Intelligent','Intelligent','Honest','Honest'),caption = "``Pastor'' Treatment Effects Controlling for Unbalanced Covariates",insert.bottom=bottom.note,booktabs = F, ctable = T, where = "htp",cgroup='DV',n.cgroup=8,landscape=T)

# =======
# Table 8
# =======

reg.control.doctor.vote<-lm(Q10_1~Q10_treat+PSDB,data=survey)
reg.control.doctor.comp<-lm(Q10_2~Q10_treat+PSDB,data=survey)
reg.control.doctor.intel<-lm(Q10_3~Q10_treat+PSDB,data=survey)
reg.control.doctor.honest<-lm(Q10_4~Q10_treat+PSDB,data=survey)

se.control.doctor.vote<-paste('(',round(coef(summary(reg.control.doctor.vote))[,2],2),')',sep='')
se.control.doctor.comp<-paste('(',round(coef(summary(reg.control.doctor.comp))[,2],2),')',sep='')
se.control.doctor.intel<-paste('(',round(coef(summary(reg.control.doctor.intel))[,2],2),')',sep='')
se.control.doctor.honest<-paste('(',round(coef(summary(reg.control.doctor.honest))[,2],2),')',sep='')

se.nocontrol.doctor.vote<-c(paste('(',round(coef(summary(reg.doctor.all))[,2],2),')',sep=''),'')
se.nocontrol.doctor.comp<-c(paste('(',round(coef(summary(reg.doctor.comp.all))[,2],2),')',sep=''),'')
se.nocontrol.doctor.intel<-c(paste('(',round(coef(summary(reg.doctor.intel.all))[,2],2),')',sep=''),'')
se.nocontrol.doctor.honest<-c(paste('(',round(coef(summary(reg.doctor.honest.all))[,2],2),')',sep=''),'')

spacer.control.doctor.vote<-ifelse(round(coef(summary(reg.control.doctor.vote))[,1],2)<0,'','\\ ')
spacer.control.doctor.comp<-ifelse(round(coef(summary(reg.control.doctor.comp))[,1],2)<0,'','\\ ')
spacer.control.doctor.intel<-ifelse(round(coef(summary(reg.control.doctor.intel))[,1],2)<0,'','\\ ')
spacer.control.doctor.honest<-ifelse(round(coef(summary(reg.control.doctor.honest))[,1],2)<0,'','\\ ')

spacer.nocontrol.doctor.vote<-ifelse(round(coef(summary(reg.doctor.all))[,1],2)<0,'','\\ ')
spacer.nocontrol.doctor.comp<-ifelse(round(coef(summary(reg.doctor.comp.all))[,1],2)<0,'','\\ ')
spacer.nocontrol.doctor.intel<-ifelse(round(coef(summary(reg.doctor.intel.all))[,1],2)<0,'','\\ ')
spacer.nocontrol.doctor.honest<-ifelse(round(coef(summary(reg.doctor.honest.all))[,1],2)<0,'','\\ ')

est.control.doctor.vote<-paste(spacer.control.doctor.vote,round(coef(summary(reg.control.doctor.vote))[,1],2),sep='')
est.control.doctor.comp<-paste(spacer.control.doctor.comp,round(coef(summary(reg.control.doctor.comp))[,1],2),sep='')
est.control.doctor.intel<-paste(spacer.control.doctor.intel,round(coef(summary(reg.control.doctor.intel))[,1],2),sep='')
est.control.doctor.honest<-paste(spacer.control.doctor.honest,round(coef(summary(reg.control.doctor.honest))[,1],2),sep='')

est.nocontrol.doctor.vote<-c(paste(spacer.nocontrol.doctor.vote,round(coef(summary(reg.doctor.all))[,1],2),sep=''),'')
est.nocontrol.doctor.comp<-c(paste(spacer.nocontrol.doctor.comp,round(coef(summary(reg.doctor.comp.all))[,1],2),sep=''),'')
est.nocontrol.doctor.intel<-c(paste(spacer.nocontrol.doctor.intel,round(coef(summary(reg.doctor.intel.all))[,1],2),sep=''),'')
est.nocontrol.doctor.honest<-c(paste(spacer.nocontrol.doctor.honest,round(coef(summary(reg.doctor.honest.all))[,1],2),sep=''),'')

control.doctor.vote<-c(as.vector(rbind(est.control.doctor.vote,se.control.doctor.vote)),reg.control.doctor.vote$rank+reg.control.doctor.vote$df.residual)
control.doctor.comp<-c(as.vector(rbind(est.control.doctor.comp,se.control.doctor.comp)),reg.control.doctor.comp$rank+reg.control.doctor.comp$df.residual)
control.doctor.intel<-c(as.vector(rbind(est.control.doctor.intel,se.control.doctor.intel)),reg.control.doctor.intel$rank+reg.control.doctor.intel$df.residual)
control.doctor.honest<-c(as.vector(rbind(est.control.doctor.honest,se.control.doctor.honest)),reg.control.doctor.honest$rank+reg.control.doctor.honest$df.residual)

nocontrol.doctor.vote<-c(as.vector(rbind(est.nocontrol.doctor.vote,se.nocontrol.doctor.vote)),reg.doctor.all$rank+reg.doctor.all$df.residual)
nocontrol.doctor.comp<-c(as.vector(rbind(est.nocontrol.doctor.comp,se.nocontrol.doctor.comp)),reg.doctor.comp.all$rank+reg.doctor.comp.all$df.residual)
nocontrol.doctor.intel<-c(as.vector(rbind(est.nocontrol.doctor.intel,se.nocontrol.doctor.intel)),reg.doctor.intel.all$rank+reg.doctor.intel.all$df.residual)
nocontrol.doctor.honest<-c(as.vector(rbind(est.nocontrol.doctor.honest,se.nocontrol.doctor.honest)),reg.doctor.honest.all$rank+reg.doctor.honest.all$df.residual)

results.table.doctor.control<-data.frame(nocontrol.doctor.vote, control.doctor.vote, nocontrol.doctor.comp, control.doctor.comp, nocontrol.doctor.intel, control.doctor.intel, nocontrol.doctor.honest, control.doctor.honest)

bottom.note<-paste("NOTE: Entries are OLS regression coefficients with estimated standard errors in parentheses.")

results.table.doctor.control.latex<-latex(results.table.doctor.control,file='results_table_doctor_control.tex',collabel.just=rep('l',8),col.just = rep('l',8),rowname=c('Intercept','','Treatment','','PSDB','','\\textit{N}'),rowlabel='',colheads=c('Vote','Vote','Competent','Competent','Intelligent','Intelligent','Honest','Honest'),caption = "``Doctor'' Treatment Effects Controlling for Unbalanced Covariates",insert.bottom=bottom.note,booktabs = F, ctable = T, where = "htp",cgroup='DV',n.cgroup=8,landscape=T)

# =======
# Table 9
# =======

reg.pastor<-lrm(pastor~clergy.cands.muni+evangelical+catholic+factor(year),x=T,y=T,data=clergy)
cluster.pastor<-robcov(reg.pastor,cluster=paste(clergy$codeibge))

se.pastor.name<-paste('(',round(sqrt(diag(cluster.pastor$var)),2),')',sep='')
space.pastor.name<-ifelse(round(cluster.pastor$coefficients,2)<0,'','\\ ')
est.pastor.name<-paste(space.pastor.name,round(cluster.pastor$coefficients,2),sep='')

results.table.pastor.name<-data.frame(c(as.vector(rbind(est.pastor.name,se.pastor.name)),sum(cluster.pastor$freq)))
bottom.note<-paste("NOTE: Entries are logistic regression coefficients with estimated standard errors (clustered on municipality) in parentheses.")

results.table.pastor.name.latex<-latex(results.table.pastor.name,file='results_table_pastor_name.tex',collabel.just='l',col.just = 'l',rowname=c('Intercept','','Number of Clergy Candidates','','Percent Evangelical','','Percent Catholic','','Year 2004','','Year 2008','','Year 2012','','\\textit{N}'),rowlabel='',colheads='',caption = "Municipal-Level Predictors of Using a ``Pastor'' Electoral Name",insert.bottom=bottom.note,booktabs = F, ctable = T, where = "htp",landscape=F)
